package com.czy.seed.mvc.home.controller;

import com.alibaba.fastjson.JSONObject;
import com.czy.seed.mvc.home.entity.AddressManager;
import com.czy.seed.mvc.home.service.AddressManagerService;
import com.czy.seed.mvc.util.PrincipalUtil;
import com.czy.seed.mvc.util.Res;
import com.czy.seed.mybatis.base.QueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author  wang
 * @date 20180222
 * */
@RestController
@RequestMapping("/home/addressManager")
public class AddressManagerController {

    @Autowired
    private AddressManagerService addressManagerService;


    public static Logger logger = LoggerFactory.getLogger(AddressManagerController.class);



    /**
     * 查询所有地址，不带任何条件
     * */
    @RequestMapping("/selectAll")
    public Res selectAll() {
        QueryParams queryParams = new QueryParams(AddressManager.class);
        List<AddressManager> addressManagers = null;
        try {
            addressManagers = addressManagerService.selectListByParams(queryParams);
        } catch (Exception e) {

            logger.error("查询所有地址信息出错：{}", e);
        }
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        pageInfo.put("addressManagers", addressManagers);
        return Res.ok(pageInfo);
    }
/**
 * 查询单个地址
 * */
    @RequestMapping("/selectOne")
    public Res selectOne(long id) {
        AddressManager addressManagers = null;
        try {
            addressManagers = addressManagerService.selectByPrimaryKey(id);
        } catch (Exception e) {

            logger.error("查询地址信息出错：{}", e);
        }
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        pageInfo.put("addressManagers", addressManagers);
        return Res.ok(pageInfo);
    }
    /**
     * 查询跟自己有关的地址
     * */
    @RequestMapping("/selectByUserAndType")
    public Res selectByUserAndType() {
        QueryParams queryParams = new QueryParams(AddressManager.class);
        QueryParams.Criteria criteria = queryParams.createCriteria();
        queryParams.orderBy("createTime").desc();
        criteria.andEqualTo("userId", PrincipalUtil.getLoginUser().getId());
        List<AddressManager> addressManagers = null;
        try {
            addressManagers = addressManagerService.selectListByParams(queryParams);
        } catch (Exception e) {

            logger.error("查询所有地址信息出错：{}", e);
        }
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        pageInfo.put("addressManagers", addressManagers);
        return Res.ok(pageInfo);
    }
    /**
     * 查询跟自己有关的默认地址
     * */
    @RequestMapping("/selectByUserAndTypeCheck")
    public Res selectByUserAndTypeCheck() {
        QueryParams queryParams = new QueryParams(AddressManager.class);
        QueryParams.Criteria criteria = queryParams.createCriteria();
        criteria.andEqualTo("userId", PrincipalUtil.getLoginUser().getId());
        criteria.andEqualTo("ischeck", true);
        List<AddressManager> addressManagers = null;
        try {
            addressManagers = addressManagerService.selectListByParams(queryParams);
        } catch (Exception e) {
            logger.error("查询所有地址信息出错：{}", e);
        }
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        pageInfo.put("addressManagers", addressManagers);
        return Res.ok(pageInfo);
    }


    @RequestMapping("/updateAddress")
    public Res updateAddress(HttpServletRequest request) {

        Map<String, String> pageInfo = new HashMap<String, String>(16);
        String object = request.getParameter("data");
        AddressManager addressManager = JSONObject.parseObject(object, AddressManager.class);
        addressManager.setUpdateBy(PrincipalUtil.getLoginUser().getId());
        addressManager.setUpdateTime(new Date());
        try {
            addressManagerService.updateByPrimaryKey(addressManager);
        } catch (Exception e) {
            logger.error("更新地址信息出错：{}", e);
        }
        return Res.ok();
    }
    @RequestMapping("/updateAddressFlag")
    public Res updateAddressFlag(HttpServletRequest request) {
        String object = request.getParameter("addressData");
        AddressManager addressManager = JSONObject.parseObject(object, AddressManager.class);
        addressManager.setUpdateBy(PrincipalUtil.getLoginUser().getId());
        addressManager.setUpdateTime(new Date());
        addressManager.setIscheck(true);
        //先将所有的都设置为非默认
        addressManagerService.updateByParentId(addressManager.getUserId());
        try {
            addressManagerService.updateByPrimaryKey(addressManager);
        } catch (Exception e) {

            logger.error("更新地址信息出错：{}", e);
        }
        return Res.ok();
    }
    @RequestMapping("/insertAddress")
    public Res insertAddress(HttpServletRequest request) {
        String address = request.getParameter("data");
        AddressManager addressManager = new AddressManager();
        addressManager.setCreateBy(PrincipalUtil.getLoginUser().getId());
        addressManager.setCreateTime(new Date());
        addressManager.setAddress(address);
        addressManager.setIscheck(false);
        addressManager.setUserId(PrincipalUtil.getLoginUser().getId());
        if("unit".equals(PrincipalUtil.getLoginUserRoles().get(0).getAuthority())){
            addressManager.setUserType(2);
        }
        if("office".equals(PrincipalUtil.getLoginUserRoles().get(0).getAuthority())){
            addressManager.setUserType(1);
        }
        if("finalist".equals(PrincipalUtil.getLoginUserRoles().get(0).getAuthority())){
            addressManager.setUserType(3);
        }
        try {
            addressManagerService.insert(addressManager);
        } catch (Exception e) {
            logger.error("新增地址信息出错：{}", e);
        }
        return Res.ok();
    }
    @RequestMapping("/deleteAddress")
    public Res deleteAddress(HttpServletRequest request) {
        String object = request.getParameter("data");
        AddressManager addressManager = JSONObject.parseObject(object, AddressManager.class);
        QueryParams queryParams = new QueryParams(AddressManager.class);
        QueryParams.Criteria criteria = queryParams.createCriteria();
        criteria.andEqualTo("id", addressManager.getId());
        try {
            addressManagerService.deleteByParams(queryParams);
        } catch (Exception e) {
            logger.error("删除地址信息出错：{}", e);
        }
        return Res.ok();
    }

}
